<template>
  <div class="attractions">
    <headers></headers>
    <div class="my-orders-container">
      <!-- 页面标题区域 -->
      <div class="page-header">
        <h2 class="page-title">我的预订</h2>
        <p class="page-subtitle">管理您的景点与酒店预订记录</p>
      </div>
      
      <!-- 标签页区域 -->
      <el-card class="tab-card">
        <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
          <!-- 景点订单标签页 -->
          <el-tab-pane label="景点预订" name="first">
            <div class="tab-content" v-loading="loading">
              <!-- 订单列表 -->
              <div class="order-grid">
                <div 
                  v-for="(item, index) in tableData" 
                  :key="index" 
                  class="order-item"
                  @click="toOrderInfo(index)"
                >
                  <!-- 订单状态标签 -->
                  <div class="order-status-badge" :class="getStatusClass(item.state)">
                    {{ getStatusText(item.state) }}
                  </div>
                  
                  <!-- 图片区域 -->
                  <div class="order-image">
                    <el-image 
                      :src="item.images.split(',')[0]" 
                      fit="cover"
                    >
                      <div slot="error" class="image-placeholder">
                        <i class="el-icon-picture-outline"></i>
                      </div>
                    </el-image>
                  </div>
                  
                  <!-- 订单内容 -->
                  <div class="order-content">
                    <h3 class="order-title">{{ item.name }}</h3>
                    
                    <div class="order-info">
                      <span class="info-item">
                        <i class="el-icon-date"></i>
                        预订日期: {{ item.time }}
                      </span>
                      <span class="info-item">
                        <i class="el-icon-tickets"></i>
                        预订数量: {{ item.num }}张
                      </span>
                    </div>
                    
                    <p class="order-intro">{{ truncateText(item.introduce, 60) }}</p>
                    
                    <div class="order-actions">
                      <el-button 
                        type="primary" 
                        size="mini" 
                        plain
                        @click.stop="toOrderInfo(index)"
                      >查看详情</el-button>
                      
                      <el-button 
                        v-if="canCancel(item.state)"
                        type="danger" 
                        size="mini" 
                        plain
                        @click.stop="promptCancelOrder(item.id, 'attraction')"
                      >取消预订</el-button>
                    </div>
                  </div>
                </div>
              </div>
              
              <!-- 空状态 -->
              <el-empty 
                v-if="tableData.length === 0" 
                description="暂无景点预订记录"
              >
                <el-button type="primary" @click="$router.push('/attractions')">去预订景点</el-button>
              </el-empty>
              
              <!-- 分页器 -->
              <div class="pagination-container">
                <el-pagination
                  background
                  :current-page.sync="search.pageNumber"
                  :page-size="search.pageSize"
                  layout="total, prev, pager, next"
                  @current-change="handleCurrentChange"
                  :total="total"
                >
                </el-pagination>
              </div>
            </div>
          </el-tab-pane>
          
          <!-- 酒店订单标签页 -->
          <el-tab-pane label="酒店预订" name="second">
            <div class="tab-content" v-loading="loading1">
              <!-- 订单列表 -->
              <div class="order-grid">
                <div 
                  v-for="(item, index) in tableData1" 
                  :key="index" 
                  class="order-item"
                  @click="toOrderInfo1(index)"
                >
                  <!-- 订单状态标签 -->
                  <div class="order-status-badge" :class="getStatusClass(item.state)">
                    {{ getStatusText(item.state) }}
                  </div>
                  
                  <!-- 图片区域 -->
                  <div class="order-image">
                    <el-image 
                      :src="item.images.split(',')[0]" 
                      fit="cover"
                    >
                      <div slot="error" class="image-placeholder">
                        <i class="el-icon-picture-outline"></i>
                      </div>
                    </el-image>
                  </div>
                  
                  <!-- 订单内容 -->
                  <div class="order-content">
                    <h3 class="order-title">{{ item.name }}</h3>
                    
                    <div class="order-info">
                      <span class="info-item">
                        <i class="el-icon-date"></i>
                        入住日期: {{ item.time }}
                      </span>
                      <span class="info-item">
                        <i class="el-icon-house"></i>
                        预订房间: {{ item.num }}间
                      </span>
                    </div>
                    
                    <p class="order-intro">{{ truncateText(item.introduce, 60) }}</p>
                    
                    <div class="order-actions">
                      <el-button 
                        type="primary" 
                        size="mini" 
                        plain
                        @click.stop="toOrderInfo1(index)"
                      >查看详情</el-button>
                      
                      <el-button 
                        v-if="canCancel(item.state)"
                        type="danger" 
                        size="mini" 
                        plain
                        @click.stop="promptCancelOrder(item.id, 'hotel')"
                      >取消预订</el-button>
                    </div>
                  </div>
                </div>
              </div>
              
              <!-- 空状态 -->
              <el-empty 
                v-if="tableData1.length === 0" 
                description="暂无酒店预订记录"
              >
                <el-button type="primary" @click="$router.push('/hotel')">去预订酒店</el-button>
              </el-empty>
              
              <!-- 分页器 -->
              <div class="pagination-container">
                <el-pagination
                  background
                  :current-page.sync="search1.pageNumber"
                  :page-size="search1.pageSize"
                  layout="total, prev, pager, next"
                  @current-change="handleCurrentChange1"
                  :total="total1"
                >
                </el-pagination>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    
    <!-- 景点预订详情对话框 -->
    <el-dialog
      title="景点预订详情"
      :visible.sync="dialogVisible"
      width="500px"
      custom-class="order-detail-dialog"
    >
      <div class="order-detail-content">
        <!-- 状态显示区域 -->
        <div class="detail-status-section">
          <div class="status-title">当前状态</div>
          <div class="status-tag" :class="getStatusClass(state)">
            {{ getStatusText(state) }}
          </div>
        </div>
        
        <!-- 预订详情区域 -->
        <div class="detail-info-section">
          <div class="section-title">预订信息</div>
          
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">预订数量</span>
              <el-input-number 
                disabled 
                size="small" 
                v-model="num" 
                :min="1" 
                :max="10"
                class="info-value"
              ></el-input-number>
            </div>
            
            <div class="info-item">
              <span class="info-label">预订日期</span>
              <el-date-picker 
                disabled 
                size="small" 
                v-model="date1"
                type="date"
                placeholder="选择预约日期"
                class="info-value"
              ></el-date-picker>
            </div>
          </div>
        </div>
        
        <!-- 游客信息区域 -->
        <div class="detail-visitors-section">
          <div class="section-title">游客信息</div>
          
          <div 
            v-for="(item, index) in people" 
            :key="index"
            class="visitor-item"
          >
            <div class="visitor-number">游客 {{ index + 1 }}</div>
            <div class="visitor-details">
              <el-row :gutter="10">
                <el-col :span="8">
                  <div class="detail-input-item">
                    <div class="input-label">姓名</div>
                    <el-input disabled size="small" v-model="item.name"></el-input>
                  </div>
                </el-col>
                
                <el-col :span="8">
                  <div class="detail-input-item">
                    <div class="input-label">电话</div>
                    <el-input disabled size="small" v-model="item.tel"></el-input>
                  </div>
                </el-col>
                
                <el-col :span="8">
                  <div class="detail-input-item">
                    <div class="input-label">身份证</div>
                    <el-input disabled size="small" v-model="item.idCard"></el-input>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </div>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="close" size="small">关 闭</el-button>
        <el-button 
          type="danger" 
          v-if="canCancel(state)"
          @click="editSysAttractionOrder" 
          size="small"
        >取消预订</el-button>
      </div>
    </el-dialog>
    
    <!-- 酒店预订详情对话框 -->
    <el-dialog
      title="酒店预订详情"
      :visible.sync="dialogVisible1"
      width="500px"
      custom-class="order-detail-dialog"
    >
      <div class="order-detail-content">
        <!-- 状态显示区域 -->
        <div class="detail-status-section">
          <div class="status-title">当前状态</div>
          <div class="status-tag" :class="getStatusClass(state)">
            {{ getStatusText(state) }}
          </div>
        </div>
        
        <!-- 预订详情区域 -->
        <div class="detail-info-section">
          <div class="section-title">预订信息</div>
          
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">房间数量</span>
              <el-input-number 
                disabled 
                size="small" 
                v-model="num" 
                :min="1" 
                :max="10"
                class="info-value"
              ></el-input-number>
            </div>
            
            <div class="info-item">
              <span class="info-label">入住日期</span>
              <el-date-picker 
                disabled 
                size="small" 
                v-model="date1"
                type="date"
                placeholder="选择入住日期"
                class="info-value"
              ></el-date-picker>
            </div>
          </div>
        </div>
        
        <!-- 入住人信息区域 -->
        <div class="detail-visitors-section">
          <div class="section-title">入住人信息</div>
          
          <div class="visitor-item">
            <div class="visitor-details">
              <el-row :gutter="10">
                <el-col :span="8">
                  <div class="detail-input-item">
                    <div class="input-label">姓名</div>
                    <el-input disabled size="small" v-model="people1.name"></el-input>
                  </div>
                </el-col>
                
                <el-col :span="8">
                  <div class="detail-input-item">
                    <div class="input-label">电话</div>
                    <el-input disabled size="small" v-model="people1.tel"></el-input>
                  </div>
                </el-col>
                
                <el-col :span="8">
                  <div class="detail-input-item">
                    <div class="input-label">身份证</div>
                    <el-input disabled size="small" v-model="people1.idCard"></el-input>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </div>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="close1" size="small">关 闭</el-button>
        <el-button 
          type="danger" 
          v-if="canCancel(state)"
          @click="editSysHotelOrder" 
          size="small"
        >取消预订</el-button>
      </div>
    </el-dialog>
    
    <!-- 确认取消对话框 -->
    <el-dialog
      title="确认取消"
      :visible.sync="cancelDialogVisible"
      width="400px"
    >
      <div class="cancel-dialog-content">
        <i class="el-icon-warning-outline warning-icon"></i>
        <p class="cancel-message">您确定要取消此预订吗？</p>
        <p class="cancel-tips">取消申请提交后需要等待审核，请谨慎操作。</p>
      </div>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogVisible = false" size="small">取 消</el-button>
        <el-button type="danger" @click="confirmCancel" size="small">确认取消</el-button>
      </div>
    </el-dialog>
    
    <bottoms></bottoms>
  </div>
</template>

<script>
import { getSysHotelOrderPage, getSysAttractionOrderPage, editSysAttractionOrder, editSysHotelOrder } from '../../api/api'
import headers from '@/components/header'
import bottoms from '@/components/bottom'

export default {
  data() {
    return {
      search: {
        userId: "",
        pageSize: 12,
        pageNumber: 1,
      },
      search1: {
        userId: "",
        pageSize: 12,
        pageNumber: 1,
      },
      total: 100,
      tableData: [],
      total1: 100,
      tableData1: [],
      activeName: "first",
      id: "",
      state: "",
      num: "",
      date1: "",
      people: [],
      people1: {},
      dialogVisible: false,
      dialogVisible1: false,
      loading: false,
      loading1: false,
      // 新增的取消确认对话框
      cancelDialogVisible: false,
      cancelType: '', // 'attraction' 或 'hotel'
      cancelId: ''
    }
  },
  components: {
    headers,
    bottoms
  },
  methods: {
    // 处理取消预订
    promptCancelOrder(id, type) {
      this.cancelId = id;
      this.cancelType = type;
      this.cancelDialogVisible = true;
    },
    
    // 确认取消预订
    confirmCancel() {
      if (this.cancelType === 'attraction') {
        this.cancelAttractionOrder(this.cancelId);
      } else if (this.cancelType === 'hotel') {
        this.cancelHotelOrder(this.cancelId);
      }
      this.cancelDialogVisible = false;
    },
    
    // 取消景点预订
    cancelAttractionOrder(id) {
      const param = {
        id: id,
        state: 2,
      };
      
      editSysAttractionOrder(param).then(res => {
        if (res.code == 1000) {
          this.$message({
            message: '取消申请成功,请等待审核通过',
            type: 'success'
          });
          this.getSysAttractionOrderPage();
        }
      });
    },
    
    // 取消酒店预订
    cancelHotelOrder(id) {
      const param = {
        id: id,
        state: 2,
      };
      
      editSysHotelOrder(param).then(res => {
        if (res.code == 1000) {
          this.$message({
            message: '取消申请成功,请等待审核通过',
            type: 'success'
          });
          this.getSysHotelOrderPage();
        }
      });
    },
    
    // 原有的取消景点预订方法
    editSysAttractionOrder() {
      this.cancelAttractionOrder(this.id);
      this.close();
    },
    
    // 原有的取消酒店预订方法
    editSysHotelOrder() {
      this.cancelHotelOrder(this.id);
      this.close1();
    },
    
    // 获取订单状态显示文本
    getStatusText(state) {
      const statusMap = {
        0: '未确认',
        1: '已确认',
        2: '取消中',
        3: '已取消',
        4: '取消失败',
        5: '已使用'
      };
      return statusMap[state] || '未知状态';
    },
    
    // 获取订单状态CSS类
    getStatusClass(state) {
      const classMap = {
        0: 'status-pending',
        1: 'status-confirmed',
        2: 'status-cancelling',
        3: 'status-cancelled',
        4: 'status-cancel-failed',
        5: 'status-used'
      };
      return classMap[state] || '';
    },
    
    // 判断是否可以取消订单
    canCancel(state) {
      return ![2, 3, 4, 5].includes(Number(state));
    },
    
    // 截断文本
    truncateText(text, length) {
      if (!text) return '';
      return text.length > length ? text.substring(0, length) + '...' : text;
    },
    
    toOrderInfo(index) {
      const data = this.tableData[index];
      this.id = data.id;
      this.people = JSON.parse(data.people);
      this.num = data.num;
      this.date1 = data.time;
      this.state = data.state;
      this.dialogVisible = true;
    },
    
    toOrderInfo1(index) {
      const data = this.tableData1[index];
      this.id = data.id;
      this.people1 = JSON.parse(data.people);
      this.num = data.num;
      this.date1 = data.time;
      this.state = data.state;
      this.dialogVisible1 = true;
    },
    
    close() {
      this.id = "";
      this.people = [];
      this.num = "";
      this.date1 = "";
      this.state = "";
      this.dialogVisible = false;
    },
    
    close1() {
      this.id = "";
      this.people1 = {};
      this.num = "";
      this.date1 = "";
      this.state = "";
      this.dialogVisible1 = false;
    },
    
    getSysAttractionOrderPage() {
      this.loading = true;
      getSysAttractionOrderPage(this.search).then(res => {
        if (res.code == 1000) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      }).finally(() => {
        this.loading = false;
      });
    },
    
    getSysHotelOrderPage() {
      this.loading1 = true;
      getSysHotelOrderPage(this.search1).then(res => {
        if (res.code == 1000) {
          this.tableData1 = res.data.records;
          this.total1 = res.data.total;
        }
      }).finally(() => {
        this.loading1 = false;
      });
    },
    
    handleCurrentChange(val) {
      this.search.pageNumber = val;
      this.getSysAttractionOrderPage();
    },
    
    handleCurrentChange1(val) {
      this.search1.pageNumber = val;
      this.getSysHotelOrderPage();
    },
    
    handleClick(tab, event) {
      console.log(tab, event);
    }
  },
  mounted() {
    try {
      const userInfo = JSON.parse(window.localStorage.getItem("user_info"));
      if (userInfo && userInfo.id) {
        this.search.userId = userInfo.id;
        this.search1.userId = userInfo.id;
        this.getSysAttractionOrderPage();
        this.getSysHotelOrderPage();
      } else {
        this.$message.error('请先登录');
        this.$router.push('/login');
      }
    } catch (error) {
      this.$message.error('获取用户信息失败，请重新登录');
      this.$router.push('/login');
    }
  }
}
</script>

<style scoped>
/* 保留原有样式导入 */
@import url('../../assets/css/attractions.css');

/* 页面容器 */
.my-orders-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

/* 页面标题 */
.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-title {
  font-size: 28px;
  color: #303133;
  margin-bottom: 8px;
}

.page-subtitle {
  font-size: 16px;
  color: #909399;
  margin: 0;
}

/* 标签卡片 */
.tab-card {
  margin-bottom: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

/* 标签内容区域 */
.tab-content {
  padding: 20px 0;
  min-height: 400px;
}

/* 订单列表布局 */
.order-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
}

/* 订单项 */
.order-item {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  transition: all 0.3s;
  cursor: pointer;
}

.order-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.15);
}

/* 订单状态标签 */
.order-status-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  color: white;
  z-index: 2;
}

.status-pending {
  background-color: #E6A23C;
}

.status-confirmed {
  background-color: #67C23A;
}

.status-cancelling {
  background-color: #F56C6C;
}

.status-cancelled {
  background-color: #909399;
}

.status-cancel-failed {
  background-color: #F56C6C;
}

.status-used {
  background-color: #409EFF;
}

/* 订单图片 */
.order-image {
  height: 180px;
  overflow: hidden;
}

.el-image {
  width: 100%;
  height: 100%;
  transition: transform 0.5s;
}

.order-item:hover .el-image {
  transform: scale(1.05);
}

/* 图片占位符 */
.image-placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 30px;
}

/* 订单内容 */
.order-content {
  padding: 15px;
}

.order-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 10px 0;
  line-height: 1.4;
  /* 单行文本截断 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 订单信息 */
.order-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.info-item {
  color: #606266;
  font-size: 13px;
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

.info-item i {
  margin-right: 5px;
}

/* 订单介绍 */
.order-intro {
  color: #909399;
  font-size: 13px;
  margin: 10px 0;
  height: 40px;
  line-height: 1.5;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* 订单操作区域 */
.order-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 15px;
}

/* 分页容器 */
.pagination-container {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

/* 详情对话框样式 */
.order-detail-dialog >>> .el-dialog__body {
  padding: 20px;
}

.order-detail-content {
  color: #606266;
}

/* 状态显示区域 */
.detail-status-section {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.status-title {
  font-weight: 500;
  margin-right: 15px;
}

.status-tag {
  padding: 5px 12px;
  border-radius: 4px;
  color: white;
  font-size: 14px;
}

/* 信息区域 */
.detail-info-section, .detail-visitors-section {
  margin-bottom: 25px;
}

.section-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 15px;
  border-bottom: 1px solid #EBEEF5;
  padding-bottom: 10px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}

.info-label {
  display: block;
  margin-bottom: 5px;
  color: #909399;
}

.visitor-item {
  background-color: #f5f7fa;
  border-radius: 4px;
  padding: 15px;
  margin-bottom: 10px;
}

.visitor-number {
  font-weight: 500;
  margin-bottom: 10px;
  color: #409EFF;
}

.detail-input-item {
  margin-bottom: 10px;
}

.input-label {
  font-size: 13px;
  color: #909399;
  margin-bottom: 5px;
}

/* 取消对话框 */
.cancel-dialog-content {
  text-align: center;
  padding: 10px 0;
}

.warning-icon {
  font-size: 48px;
  color: #E6A23C;
  margin-bottom: 15px;
}

.cancel-message {
  font-size: 18px;
  color: #303133;
  margin-bottom: 10px;
}

.cancel-tips {
  color: #909399;
  font-size: 14px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .my-orders-container {
    padding: 15px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .order-grid {
    grid-template-columns: 1fr;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>